package com.songshu.e.dubbo.api.controller;

import com.songshu.e.dubbo.api.service.HouseResourcesService;
import com.songshu.e.dubbo.api.vo.TableResult;
import com.songshu.e.dubbo.server.pojo.HouseResources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("house/resources")
public class HouseResourcesController {
    @Autowired
    private HouseResourcesService houseResourcesService;

    /**
     * 新增房源
     * houseResources : JSON数据
     */
    @PostMapping
    @RequestMapping
    public ResponseEntity<Void> save(@RequestBody HouseResources houseResources) {
        try {
            boolean bool = houseResourcesService.save(houseResources);
            if (bool) {
                //保存成功
                return ResponseEntity.status(HttpStatus.CREATED).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 查询房源列表
     * *
     *
     * @param houseResources
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping
    @ResponseBody
    public ResponseEntity<TableResult<HouseResources>> list(HouseResources houseResources,
                                                            @RequestParam(name = "currentPage",
                                                                    defaultValue = "1") Integer currentPage,
                                                            @RequestParam(name = "pageSize",
                                                                    defaultValue = "10") Integer pageSize) {
        return ResponseEntity.ok(this.houseResourcesService.queryList(houseResources, currentPage, pageSize));
    }

    //修改房源
    @PutMapping
    @ResponseBody
    public ResponseEntity<Void> update (@RequestBody HouseResources houseResources){

        try {
            System.out.println(houseResources.getId());
            boolean isUpdate = houseResourcesService.update(houseResources);
            if (isUpdate){
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * test
     */
//    @GetMapping
//    @ResponseBody
//    public ResponseEntity<String> get() {
//        return ResponseEntity.ok("OK");
//    }
}
